﻿Public Class frmMUsuario

    Public arrConsulta(8) As String
    Private arrCampos() As String = {"usuario_id", "tipousuario_id", "especialidadmedica_id", "usuario_documento", "usuario_documento", "usuario_idws", "usuario_login", "usuario_contra", "usuario_estado"}

    Private Sub frmMUsuario_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        mdiPrincipal.tsmiUsuario.Enabled = True
    End Sub

    Private Sub frmMUsuario_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        refreshList()
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click

        Me.tsbNuevo.Enabled = False
        Me.renderForm("Nuevo - Usuario", 1)
    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbConsultar.Click

        Me.tsbConsultar.Enabled = False
        Me.renderForm("Consultar - Usuario", 2)
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEditar.Click

        Dim strId As String = Me.dgvUsuario.Rows(Me.dgvUsuario.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then

            Me.tsbEditar.Enabled = False
            renderForm("Editar - Usuario", 3)
        Else

            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Usuario")
        End If

    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBorrar.Click

        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Usuario")

        If (objResult = DialogResult.Yes) Then

            Dim strId As String = Me.dgvUsuario.Rows(Me.dgvUsuario.CurrentRow.Index).Cells(0).Value.ToString

            mdlCRUD.delRecord("usuario", "usuario_id = '" & strId & "'")

            If (mdlCRUD.blnFlag = True) Then

                Me.refreshList()
            End If

        End If

    End Sub

    Private Sub tsbRecargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRecargar.Click

        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        arrConsulta(3) = ""
        arrConsulta(4) = ""
        arrConsulta(5) = ""
        arrConsulta(6) = ""
        arrConsulta(7) = ""
        arrConsulta(8) = ""
        Me.refreshList()
    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)

        Dim frmUsuario As New frmUsuario

        frmUsuario.MdiParent = mdiPrincipal
        frmUsuario.frmParent = Me

        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("TipoUsuario")
        mdlCRUD.loadRecord(dstRecord, "TipoUsuario", "tipousuario", "", "tipousuario_id")

        frmUsuario.cmbTipoUsuario.DataSource = dstRecord.Tables("TipoUsuario")
        frmUsuario.cmbTipoUsuario.DisplayMember = dstRecord.Tables("TipoUsuario").Columns(1).Caption.ToString
        frmUsuario.cmbTipoUsuario.ValueMember = dstRecord.Tables("TipoUsuario").Columns(0).Caption.ToString

        dstRecord.Tables.Add("Especialidad")
        mdlCRUD.loadRecord(dstRecord, "Especialidad", "especialidadmedica", "", "especialidadmedica_id")

        frmUsuario.cmbEspecialidad.DataSource = dstRecord.Tables("Especialidad")
        frmUsuario.cmbEspecialidad.DisplayMember = dstRecord.Tables("Especialidad").Columns(1).Caption.ToString
        frmUsuario.cmbEspecialidad.ValueMember = dstRecord.Tables("Especialidad").Columns(0).Caption.ToString

        frmUsuario.modeForm = intMode
        frmUsuario.Text = strTitle
        frmUsuario.Show()

    End Sub

    Public Sub refreshList()

        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("Usuario")

        For i As Integer = 0 To 1

            If (Me.arrConsulta(i) <> "") Then

                strCondicion &= IIf(strCondicion <> "", " AND ", "") & IIf(i = 2, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If

        Next

        mdlCRUD.loadRecord(dstRecord, "Usuario", "vistausuario", strCondicion, "usuario_id")
        Me.dgvUsuario.DataSource = dstRecord.Tables("Usuario")

        Me.dgvUsuario.Columns(0).HeaderText = "Codigo"
        Me.dgvUsuario.Columns(2).HeaderText = "Tipo Usuario"

        Me.dgvUsuario.Columns(4).HeaderText = "Especialidad"
        Me.dgvUsuario.Columns(5).HeaderText = "Doc."
        Me.dgvUsuario.Columns(6).HeaderText = "Correo"

        Me.dgvUsuario.Columns(8).HeaderText = "Usuario"


        Me.dgvUsuario.Columns(0).Width = 60
        Me.dgvUsuario.Columns(1).Visible = False
        Me.dgvUsuario.Columns(3).Visible = False
        Me.dgvUsuario.Columns(2).Width = 250
        Me.dgvUsuario.Columns(4).Width = 250
        Me.dgvUsuario.Columns(5).Width = 100
        Me.dgvUsuario.Columns(6).Width = 150
        Me.dgvUsuario.Columns(7).Visible = False
        Me.dgvUsuario.Columns(8).Width = 150
    End Sub

End Class